<?php

namespace App\Model\System;


use App\Model\Foundation\Employee;
use App\Model\Foundation\Student;
use App\Model\Foundation\Teacher;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Http\Request;

class User extends Model
{
    const  ADMIN = 1;
    const  TEACHER = 2;
    const  STUDENT = 3;

    const  LOCKED = 1;
    const  UNLOCK = 0;

    public $table = 'xusers';
    public $primaryKey = 'userid';
    const USERSALT = 'aksfnowiuasfkqwen1sfe2d';

    protected $fillable = [
        'userid',
        'type',
        'account',
        'email',
        'number',
        'islock'
    ];

    /**
     * 生成安全的密码，防拖库
     * @param $passwd
     * @return string
     */
    static function getSecurityCode($passwd)
    {
        return md5($passwd . self::USERSALT);
    }

    /**
     * 设置密码
     * @param $passwd
     */
    function setSecurityCode($passwd)
    {
        $this->password = $this->getSecurityCode($passwd);
    }

    /**
     * 设置创建者信息
     */
    function updateCreator()
    {
        $this->created_by = Token::user()->userid;
    }

    /**
     * 设置更新者信息
     */
    function updateModifier()
    {
        $this->updated_by = Token::user()->userid;
    }

    /**
     * 判断用户锁定信息
     * @param $stat
     */
    function setlock($stat)
    {
        if (intval($stat) >= 1) {
            $this->islock = self::LOCKED;
        } else {
            $this->islock = self::UNLOCK;
        }
    }

    /**
     * 设置登录Token
     */
    function setToken($id)
    {
        $token = md5($this->useraccount . $this->username . time(). self::USERSALT);
        $result = new Token();
        $result->xuserid = $id;
        $result->token = $token;
        $result->save();
        return $token;
    }

    /**
     * 生成唯一用户ID
     * @return string
     */
    static function generateID()
    {
        return base_convert(uniqid(), 16, 10);
    }

    /**
     * 修改手机号,如果手机号不存在，则更新用户表
     * @return string
     */
    static function updateMobile(Request $request, $obj, $field = 'mobile')
    {
        if ($request->has($field)) {
            $newNumber = $request->$field;
            $oldNumber = $obj->$field;
            if (self::isMobileExists($oldNumber, $newNumber)) {
                return false;
            }
            self::where('userid', $obj->userid)->update(["number" => $newNumber]);
            return true;
        }
    }

    /**
     * 检查手机号是否存在
     * @return string
     */
    static function isMobileExists($oldNumber, $newNumber)
    {
        if ($oldNumber !== $newNumber) {
            $user = self::where('number', $newNumber)->get()->toArray();
            if (!empty($user)) {
                return true;
            }
            return false;
        }
    }


    static function getUserTypes($userid)
    {
        $types = [];
        if(empty($userid)){
            return $types;
        }

        if($userid==1){
            $types[] = User::ADMIN;
        }else {
            $obj = Employee::where('userid', $userid)->get()->count();
            if ($obj >= 1) {
                $types[] = User::ADMIN;
            }
            $obj2 = Teacher::where('userid', $userid)->get()->count();
            if ($obj2 >= 1) {
                $types[] = User::TEACHER;
            }
            $obj3 = Student::where('userid', $userid)->get()->count();
            if ($obj3 >= 1) {
                $types[] = User::STUDENT;
            }
        }
        return $types;
    }
}



